#include "ModifyPatientUi.h"
#include "ui_ModifyPatientUi.h"
#include "WidgetHandler.h"
#include "BootSetting.h"
#include "MiscData.h"
#include "Database.h"
#include "HintDialog.h"
#include "LogoUtility.h"

ModifyPatientUi::ModifyPatientUi(QWidget *parent) :
    BaseWidget(parent),
    ui(new Ui::ModifyPatientUi)
{
    ui->setupUi(this);
    LogoUtility::getInstance()->addLogoLabel(ui->logo);
    QObject::connect(HintDialog::getInstance(), &HintDialog::ModifyPatientUi_HintDialog_Signal, this, &ModifyPatientUi::ModifyPatientUi_HintDialog_Slot);
}

ModifyPatientUi::~ModifyPatientUi()
{
    delete ui;
}

void ModifyPatientUi::activate()
{
    /* 通过number查询 */
    int number = RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentSelectPatientNumber;
    PatientInfo* info = Database::getInstance()->queryPatient(number);
    if (info->number < 0) {
        qDebug()<<"ModifyPatientUi, system error: no fine selected patient";
        return;
    }
    memcpy(&g_patientInfo, info, sizeof(PatientInfo));
    /* 更新ui */
    ui->number->setText(QString("%1").arg(info->number));
    ui->id->setText(info->id);
    ui->name->setText(info->name);
    ui->age->setText(QString("%1").arg(info->age));
    ui->birthday->setText(info->birthday);
    ui->gender->setText(QString("%1").arg(info->gender));
    ui->phone->setText(info->phone);
    ui->addr->setText(info->addr);
}

void ModifyPatientUi::inactivate()
{

}

void ModifyPatientUi::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}
void ModifyPatientUi::on_gohome_clicked()
{
    if (checkPatientInfo() < 0) {
        HintDialog::getInstance()->setInfo(QString(tr("信息有修改，但未保存，是否直接返回?")), ModifyPatientUi_GoHome);
        HintDialog::getInstance()->exec();
        return;
    }
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void ModifyPatientUi::on_goback_clicked()
{
    if (checkPatientInfo() < 0) {
        HintDialog::getInstance()->setInfo(QString(tr("信息有修改，但未保存，是否直接返回?")), ModifyPatientUi_GoBack);
        HintDialog::getInstance()->exec();
        return;
    }
    WidgetHandler::getInstance()->displayWidget(PatientInfoUi_Id);
}

void ModifyPatientUi::on_apply_clicked()
{
    if (checkPatientInfo() < 0) {
        HintDialog::getInstance()->setInfo(QString(tr("信息有修改，但未保存，是否清空修改并应用此患者!")), ModifyPatientUi_Apply);
        HintDialog::getInstance()->exec();
        return;
    }
    RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentApplyPatientNumber = RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentSelectPatientNumber;
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void ModifyPatientUi::on_modify_clicked()
{
    PatientInfo info;

    info.number = ui->number->text().toInt();
    info.id = ui->id->text();
    info.name = ui->name->text();
    info.age = ui->age->text().toInt();
    info.gender = ui->gender->text().toInt();
    info.birthday = ui->birthday->text();
    info.visitTime = g_patientInfo.visitTime;
    info.phone = ui->phone->text();
    info.addr = ui->addr->text();

    QString hint;
    int ret = Database::getInstance()->modifyPatient(&info);
    if (ret < 0) {
        hint = QString(tr("修改患者信息失败!"));
    } else {
        hint = QString(tr("修改患者信息成功!"));
    }
    HintDialog::getInstance()->setInfo(hint, ModifyPatientUi_Modify);
    HintDialog::getInstance()->exec();
}

void ModifyPatientUi::ModifyPatientUi_HintDialog_Slot(int type)
{

    switch (type) {
    case ModifyPatientUi_GoHome:
        qDebug()<<"ModifyPatientUi: ModifyPatientUi_GoHome";
        WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
        break;

    case ModifyPatientUi_GoBack:
        qDebug()<<"ModifyPatientUi: ModifyPatientUi_GoBack";
        WidgetHandler::getInstance()->displayWidget(PatientInfoUi_Id);
        break;

    case ModifyPatientUi_Apply:
        qDebug()<<"ModifyPatientUi: ModifyPatientUi_Apply";
        RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentApplyPatientNumber = RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentSelectPatientNumber;
        WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
        break;

    case ModifyPatientUi_Modify:
        qDebug()<<"ModifyPatientUi: ModifyPatientUi_Modify";
        WidgetHandler::getInstance()->displayWidget(PatientInfoUi_Id);
        break;
    }
}

int ModifyPatientUi::checkPatientInfo()
{
    PatientInfo info;

    info.number = ui->number->text().toInt();
    info.id = ui->id->text();
    info.name = ui->name->text();
    info.age = ui->age->text().toInt();
    info.birthday = ui->birthday->text();
    info.gender = ui->gender->text().toInt();
    info.visitTime = g_patientInfo.visitTime;
    info.phone = ui->phone->text();
    info.addr = ui->addr->text();

    if (memcmp(&info, &g_patientInfo, sizeof(PatientInfo)) != 0) {
        return -1;
    } else {
        return 0;
    }
}
